﻿package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.bl.queries.DiaDiemQueries;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter2;
import com.lv.phuotbacnam.ui.adapter.TListViewApdapterDiadiem;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.sileria.android.view.HorzListView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Xemtatcadiadiem extends Activity{
	ArrayList<ParseObject> arrTatcadiadiem = new ArrayList<ParseObject>();
	ArrayList<MyItem_Diadiem> arrDiadiem = new ArrayList<MyItem_Diadiem>();
	private int cachxem = 1;	//cach xem = 1 => hot nhat
								//cach xem = 2 => gan nhat
								//cach xem = 3 => moi tao
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.xemtatcadiadiem);
		final TextView tv_luachon = (TextView)findViewById(R.id.tv_selectnow);
		final ListView lv_tatcadiadiem = (ListView)findViewById(R.id.lv_tatcadiadiem);
		final Button btn_hotnhat = (Button)findViewById(R.id.btn_hotnhat);
		final Button btn_gannhat = (Button)findViewById(R.id.btn_gannhat);
		final Button btn_moitao = (Button)findViewById(R.id.btn_moitao);
		
		btn_hotnhat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cachxem = 1;
				
			}
		});
		
		btn_moitao.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cachxem = 3;
				
			}
		});
		
		if(cachxem==1)
		{
			// mặc định xem các điểm có lượt visit nhiều nhất
			DiaDiemQueries.getDiaDiemMostVisited(new CallBackFromParse<Object, List<ParseObject>>() { 
				@Override
				public void done(Object request, List<ParseObject> diadiems) {
					tv_luachon.setText("Địa điểm hot");
					
					for(ParseObject diadiem: diadiems){
						arrTatcadiadiem.add(diadiem);		
						// get list photo
						JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
		
						try {
							JSONObject first_photo = list_photo.getJSONObject(0);
							//first_photo.
							String a = first_photo.getString("url");
							MyItem_Diadiem item = new MyItem_Diadiem(a,diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem), diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta));
							arrDiadiem.add(item);
							//Logger.logMessage("firstPhoto:"+ a);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							//Logger.logMessage("khong co gi");
						}
						
						
					
						
						TListViewApdapterDiadiem adapterImg = new TListViewApdapterDiadiem(arrTatcadiadiem, Xemtatcadiadiem.this);
						lv_tatcadiadiem.setAdapter(adapterImg);
						
						//xử lý sự kiện onclick vào list địa điểm, mở ra activity "chitietdiadiem2.class"
						lv_tatcadiadiem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> arg0, View arg1,
									int arg2, long arg3) {
								
								 Intent intent = new Intent(Xemtatcadiadiem.this, ChitietDiadiem2.class);
								 IntentHelper.addObjectForKey(arrTatcadiadiem.get(arg2),"diadiem");
								 //Bundle bundle = new Bundle();
								 //bundle.putString("tendiadiem", arrDiadiem.get(arg2).getTendiadiem());
								 //intent.putExtra("MyPackage", bundle);
								 startActivity(intent);
							}			
							
						});
						
					}
					
				}
			});
		}
		/*
		else if (cachxem ==2)
		{
		
			// mặc định xem các điểm có lượt visit nhiều nhất
						DiaDiemQueries.getDiaDiemNearest(currentLocation, callBack)(new CallBackFromParse<Object, List<ParseObject>>() { 
							@Override
							public void done(Object request, List<ParseObject> diadiems) {
								tv_luachon.setText("Địa điểm hot");
								for(ParseObject diadiem: diadiems){
											
									// get list photo
									JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
					
									try {
										JSONObject first_photo = list_photo.getJSONObject(0);
										//first_photo.
										String a = first_photo.getString("url");
										MyItem_Diadiem item = new MyItem_Diadiem(a,diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem), diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta));
										arrDiadiem.add(item);
										//Logger.logMessage("firstPhoto:"+ a);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
										//Logger.logMessage("khong co gi");
									}
									
									
								
									
									TListViewApdapterDiadiem adapterImg = new TListViewApdapterDiadiem(arrDiadiem, Xemtatcadiadiem.this);
									lv_tatcadiadiem.setAdapter(adapterImg);
									
									//xử lý sự kiện onclick vào list địa điểm, mở ra activity "chitietdiadiem2.class"
									lv_tatcadiadiem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
										@Override
										public void onItemClick(AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											
											 Intent intent = new Intent(Xemtatcadiadiem.this, ChitietDiadiem2.class);
											 Bundle bundle = new Bundle();
											 bundle.putString("tendiadiem", arrDiadiem.get(arg2).getTendiadiem());
											 intent.putExtra("MyPackage", bundle);
											 startActivity(intent);
										}			
										
									});
									
								}
								
							}
						});
				}
				*/
		
		else if(cachxem == 3)
		{
			
						DiaDiemQueries.getDiaDiemNewest(new CallBackFromParse<Object, List<ParseObject>>() { 
							@Override
							public void done(Object request, List<ParseObject> diadiems) {
								tv_luachon.setText("Địa điểm hot");
								for(ParseObject diadiem: diadiems){
											
									// get list photo
									JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
					
									try {
										JSONObject first_photo = list_photo.getJSONObject(0);
										//first_photo.
										String a = first_photo.getString("url");
										MyItem_Diadiem item = new MyItem_Diadiem(a,diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem), diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta));
										arrDiadiem.add(item);
										//Logger.logMessage("firstPhoto:"+ a);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
										//Logger.logMessage("khong co gi");
									}
									
									
								
									
									TListViewApdapterDiadiem adapterImg = new TListViewApdapterDiadiem(arrTatcadiadiem, Xemtatcadiadiem.this);
									lv_tatcadiadiem.setAdapter(adapterImg);
									
									//xử lý sự kiện onclick vào list địa điểm, mở ra activity "chitietdiadiem2.class"
									lv_tatcadiadiem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
										@Override
										public void onItemClick(AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											
											 Intent intent = new Intent(Xemtatcadiadiem.this, ChitietDiadiem2.class);
											 IntentHelper.addObjectForKey(arrTatcadiadiem.get(arg2),"diadiem");
											 //Bundle bundle = new Bundle();
											 //bundle.putString("tendiadiem", arrDiadiem.get(arg2).getTendiadiem());
											 //intent.putExtra("MyPackage", bundle);
											 startActivity(intent);
										}			
										
									});
									
								}
								
							}
						});
		}
	}

}
